package lumis.portal.webservice.user;

import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.transaction.PortalTransactionFactory;
import lumis.portal.user.UserConfig;
import lumis.portal.webservice.WebServiceUtil;
import lumis.util.ITransaction;

/**
 * Web Service class for User operations.
 *
 * @version $Revision: 11080 $ $Date: 2009-11-17 16:15:52 -0200 (Tue, 17 Nov 2009) $
 * @since 4.0.9
 */
public class UserService
{
	/**
	 * Add a user to portal and returns its identifier.
	 * @param sessionId session identifier
	 * @param user user data to be added
	 * @param password password of user being added
	 * @return identifier (userId) of the added user
	 * @throws PortalException
	 */
	public String add(String sessionId, User user, String password) throws PortalException
	{
		ITransaction transaction = PortalTransactionFactory.createTransaction();
		try 
		{
			transaction.begin();
			UserConfig userConfig = UserConverter.convert(user);
			userConfig.setPassword(password);
			SessionConfig sessionConfig = WebServiceUtil.getSessionConfig(sessionId, transaction);
			String userId = ManagerFactory.getUserManager().add(sessionConfig, userConfig, transaction);
			transaction.commit();
			return userId;
		}
		finally 
		{
			transaction.dispose();
		}
		
	}

	/**
	 * Update a user's data.
	 * @param sessionId session identifier
	 * @param user user data to be updated
	 * @throws PortalException
	 */
	public void update(String sessionId, User user) throws PortalException
	{
		ITransaction transaction = PortalTransactionFactory.createTransaction();
		try 
		{
			transaction.begin();
			UserConfig userConfig = UserConverter.convert(user);
			SessionConfig sessionConfig = WebServiceUtil.getSessionConfig(sessionId, transaction);
			ManagerFactory.getUserManager().update(sessionConfig, userConfig, transaction);
			transaction.commit();
		}
		finally 
		{
			transaction.dispose();
		}
	}

	/**
	 * Delete a user
	 * @param sessionId session identifier
	 * @param userId identifier of user to be deleted
	 * @throws PortalException
	 */
	public void delete(String sessionId, String userId) throws PortalException
	{
		ITransaction transaction = PortalTransactionFactory.createTransaction();
		try 
		{
			transaction.begin();
			SessionConfig sessionConfig = WebServiceUtil.getSessionConfig(sessionId, transaction);
			ManagerFactory.getUserManager().delete(sessionConfig, userId, transaction);
			transaction.commit();
		}
		finally 
		{
			transaction.dispose();
		}
		
	}
	
	/**
	 * Get a {@link User} object by its identifier
	 * @param sessionId session identifier
	 * @param userId identifier of the user to get 
	 * @return user object
	 * @throws PortalException
	 */
	public User get(String sessionId, String userId) throws PortalException
	{
		ITransaction transaction = PortalTransactionFactory.createTransaction();
		try 
		{
			transaction.begin();
			SessionConfig sessionConfig = WebServiceUtil.getSessionConfig(sessionId, transaction);
			UserConfig userConfig = ManagerFactory.getUserManager().get(sessionConfig, userId, transaction);
			User user = UserConverter.convert(userConfig);
			transaction.commit();
			return user;
		}
		finally 
		{
			transaction.dispose();
		}
	}

	/**
	 * Get a user identifier by its login
	 * @param sessionId session identifier
	 * @param login user login
	 * @return user identifier
	 * @throws PortalException
	 */
	public String getUserIdByLogin(String sessionId, String login) throws PortalException
	{
		ITransaction transaction = PortalTransactionFactory.createTransaction();
		try 
		{
			transaction.begin();
			SessionConfig sessionConfig = WebServiceUtil.getSessionConfig(sessionId, transaction);
			String userId = ManagerFactory.getUserManager().getUserIdByLogin(sessionConfig, login, transaction);
			transaction.commit();
			return userId;
		}
		finally 
		{
			transaction.dispose();
		}
	}

	/**
	 * Change a user's password
	 * @param sessionId session identifier
	 * @param userId user identifier
	 * @param password new user's password
	 * @throws PortalException
	 */
	public void setPassword(String sessionId, String userId, String password) throws PortalException
	{
		ITransaction transaction = PortalTransactionFactory.createTransaction();
		try 
		{
			transaction.begin();
			SessionConfig sessionConfig = WebServiceUtil.getSessionConfig(sessionId, transaction);
			ManagerFactory.getUserManager().setPassword(sessionConfig, userId, password, transaction);
			transaction.commit();
		}
		finally 
		{
			transaction.dispose();
		}
	}
}
